<?php

namespace app\home\controller;

use app\common\controller\DingApi;
use app\company\service\DataService;
use library\Controller;
use think\Db;

use function Qiniu\json_decode;

/**
 * 钉钉员工列表管理
 * Class User
 * @package app\worker\controller
 */
class DingUser extends Controller
{
    /**
     * 绑定当前数据表
     * @var string
     */
    protected $table = 'ding_user';

    /**
     * 钉钉员工列表
     * @auth true
     * @menu true
     */
    public function index()
    {
        $this->title = '员工列表';
        $this->_query($this->table)
        ->alias('du1')
        ->join('ding_user du2', 'du2.userid=du1.manager_userid', 'left')
        ->dateBetween('du1.hired_date#hired_date')
        ->like('du1.name#name')
        ->field('du1.*,du2.name as manager_name')
        ->group('du1.id')
        ->page();
    }

    protected function _index_page_filter(&$data) {
        foreach($data as &$v){
            $v['department_name_list'] = $v['department_name_list'] ? json_decode($v['department_name_list'], true) : [];
            $v['role_list'] = $v['role_list'] ? json_decode($v['role_list'], true) : [];
        }
        // halt($data);
    }

    /**
     * 同步员工列表信息
     * @auth true
     */
    public function get_user()
    {
        $dingApi = new DingApi();
        //获取在职员工id列表
        $userids = $this->queryonjob();
        $user_info = [];
        foreach($userids as $v){
            $data = [
                'userid' => $v
            ];
            $user_get = $dingApi->user_get($data)['result'];
            /**
             * userid 员工的userid
             * unionid 员工在当前开发者企业账号范围内的唯一标识。
             * name 员工名称
             * avatar 头像
             * manager_userid 员工的直属主管userid
             * mobile 手机号码
             * title 职位
             * email 员工邮箱
             * org_email 员工的企业邮箱
             * dept_id_list 所属部门ID列表
             * hired_date 入职时间，Unix时间戳，单位毫秒
             * active 是否激活了钉钉
             * senior 是否为企业的高管
             * admin 是否为企业的管理员
             * boss 是否为企业的老板
             * role_list 角色列表
             *  id  角色ID
             *  name 角色名称
             *  group_name 角色组名称
             */
            $dept_id_list = $user_get['dept_id_list'] ?? [];
            //按所属部门ID列表获取部门名称列表
            $department_name_list = [];
            $dingApi = new DingApi();
            foreach($dept_id_list as $vo){
                $data_info = [
                    'dept_id' => $vo
                ];
               $department_name_list[] = $dingApi->department_get($data_info)['result']['name'];
            }
            $role_list = isset($user_get['role_list']) ? json_encode($user_get['role_list']) : '';
            $user_info[] = [
                'userid' => $user_get['userid'] ?? '',
                'unionid' => $user_get['unionid'] ?? '',
                'name' => $user_get['name'] ?? '',
                'avatar' => $user_get['avatar'] ?? '',
                'manager_userid' => $user_get['manager_userid'] ?? '',
                'mobile' => $user_get['mobile'] ?? '',
                'title' => $user_get['title'] ?? '',
                'email' => $user_get['email'] ?? '',
                'org_email' => $user_get['org_email'] ?? '',
                'dept_id_list' => json_encode($dept_id_list),
                'hired_date' => isset($user_get['hired_date']) ? date('Y-m-d H:i:s', $user_get['hired_date']/1000) : '',
                'active' => $user_get['active'] ?? '',
                'senior' => $user_get['senior'] ?? '',
                'admin' => $user_get['admin'] ?? '',
                'boss' => $user_get['boss'] ?? '',
                'role_list' =>  $role_list,
                'department_name_list' => json_encode($department_name_list),
            ];
        }
        Db::name($this->table)->where('1=1')->delete();//删除旧的
        Db::name($this->table)->insertAll($user_info);
        $this->success('同步成功');
    }

    //循环获取在职员工列表
    public function queryonjob($data_list = [], $offset = 0, $size = 50){
        $dingApi = new DingApi();
        $data = [
            'status_list' => '3',
            'offset' => $offset,
            'size' => $size,
        ];
        $res = $dingApi->queryonjob($data);
        $data_list = array_merge($data_list, $res['result']['data_list']);
        if(isset($res['result']['next_cursor'])){ 
            return $this->queryonjob($data_list, $res['result']['next_cursor']);
        }else{
            return $data_list;
        }
    }
}
